package com.itheima.leetcode;

import java.util.*;

/**
 * 设计推特
 */
public class TwitterLeetcode355 {

    static class Twitter {

        private final Map<Integer, User> userMap = new HashMap<>();
        private static int time;

        static class Tweet {
            int id;
            int time;
            Tweet next;

            public Tweet(int id, int time, Tweet next) {
                this.id = id;
                this.time = time;
                this.next = next;
            }

            public int getId() {
                return id;
            }

            public int getTime() {
                return time;
            }
        }

        static class User {
            int id;

            public User(int id) {
                this.id = id;
            }

            Set<Integer> followees = new HashSet<>();
            Tweet head = new Tweet(-1, -1, null);
        }

        // 发布文章
        public void postTweet(int userId, int tweetId) {
            User user = userMap.computeIfAbsent(userId, User::new);
            user.head.next = new Tweet(tweetId, time++, user.head.next);
        }

        // 获取最新10篇文章（包括自己和关注者）
        public List<Integer> getNewsFeed(int userId) {
            User user = userMap.get(userId);
            if (null == user) {
                return Arrays.asList();
            }
            PriorityQueue<Tweet> queue
                    = new PriorityQueue<>(Comparator.comparingInt(Tweet::getTime).reversed());
            if (null != user.head.next) {
                queue.offer(user.head.next);
            }
            for (Integer id : user.followees) {
                User followee = userMap.get(id);
                if (null != followee.head.next) {
                    queue.offer(followee.head.next);
                }
            }
            ArrayList<Integer> res = new ArrayList<>();
            int count = 0;
            while (!queue.isEmpty() && 10 < count) {
                Tweet max = queue.poll();
                res.add(max.id);
                if (null != max.next) {
                    queue.offer(max.next);
                }
                count++;
            }
            return res;
        }

        // 新增关注
        public void follow(int followerId, int followeeId) {
            User user = userMap.computeIfAbsent(followerId, User::new);
            User followee = userMap.computeIfAbsent(followeeId, User::new);
            user.followees.add(followee.id);
        }

        // 取消关注
        public void unfollow(int followerId, int followeeId) {
            User user = userMap.get(followeeId);
            if (null != user) {
                user.followees.remove(followeeId);
            }
        }
    }
}
